package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.ClientInfoMergeBean;
import com.koron.css2.clientCenter.bean.CtmChangeInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 并户，将次客户状态变为停用，所有下级账户、用户的所属客户更换为主客户
 * 
 * @author 甄玉晴
 *
 */
public class ClientInfoMerge implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			// 获取更换客户编号
			List<ClientInfoMergeBean> list=new ArrayList<ClientInfoMergeBean>();
			String listjson = JsonUtils.objectToJson(req.getData());
			list = JsonUtils.jsonToList(listjson, ClientInfoMergeBean.class);
			if (list == null || list.size() < 2) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "只有一个客户无法操作并户", null);
			}
			ClientInfoMergeBean client = list.get(0);
			// 先选择出主客户
			for (ClientInfoMergeBean bean : list) {
				if (bean.getFlag() == 1) {
					client = bean;
					break;
				}
			}
			if (client.getCtmNo() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "没有传主客户", null);
			}
			AccountInfoMapper accountMapper = factory.getMapper(AccountInfoMapper.class);
			UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
			CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
			CtmChangeInfoBean ctmChange = new CtmChangeInfoBean();
			ctmChange.setCreateAccount(userInfo.getUserInfo().getAcount());
			ctmChange.setCreateName(userInfo.getUserInfo().getName());
			ctmChange.setAfterCtmNo(client.getCtmNo());

			//当前账期
			int accountPeriod=Integer.parseInt(CommonUtils.getYearMonth());
			for (int i = 0; i < list.size(); i++) {
				// 主客户
				if (list.get(i).getFlag() == 1) {
					continue;
				}
				// 次客户的三户关系删除。用户及账户已转移，单独存在客户没有意义
				userMapper.userRelationHstDelete(list.get(i).getCtmNo(), null, null);
				// 获取次客户的用户信息
				List<UserBean> beforeUser = userMapper.getUserInfoByCtmNo(list.get(i).getCtmNo(), UserBean.USER);
				for (UserBean user : beforeUser) {
					// 将次客户的用户及账户添加到新客户的三户关系中
					userMapper.userRelationHstAdd(Tools.getObjectId(), client.getCtmNo(), user.getUserNo(), user.getAccountNo());
				}
				// 次客户的用户表客户编号修改
				userMapper.userInfoCtmNoUpdate(list.get(i).getCtmNo(), client.getCtmNo());


				// 修改次客户的客户状态为停用
				ctmMapper.ctmInfoStatusUpdate(list.get(i).getCtmNo(),2);
/*				// 记录客户变更记录表
				ctmChange.setId(Tools.getObjectId());
				ctmChange.setBeforeCtmNo(list.get(i).getCtmNo());*/
			}
			//用户表、账单表、抄表表冗余字段本月份的才同步
			userMapper.userInfoCtmInfoUpdate(client.getCtmNo());
			userMapper.updateBillDetail(client.getCtmNo(),String.valueOf(accountPeriod));
			userMapper.updateMrInfo(client.getCtmNo(),String.valueOf(accountPeriod));

			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
		} catch (Exception e) {
			logger.error("用户变更信息查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
